﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using CoreProcess.Resources;
using CoreProcess.Utility;

namespace CoreProcess
{
    public class ResourceService
    {
        //第一个阶段应该是分析 扫描的结果 然后才能处理
        public ResourceRoot LoadResource(string resourceRootPath)
        {
            return
                 new XmlSerialize<ResourceRoot>().FromXmlFile(resourceRootPath);
        }

        public class typeItem
        {
             public string filetype { get; set; }
            public string count { get; set; }


            public override string ToString()
            {
                return filetype + "#" + count;
            }
        }

        public void GetSameResource(String resourceRootPath)
        {
            var xmlSerialize = new XmlSerialize<ResourceRoot>();
            var resourceRoot =
                xmlSerialize.FromXmlFile(resourceRootPath);
            
            var resList =
                resourceRoot.Items
                    .GroupBy(item => item.Md5Hexstr) //获得层级集合
                    .Where(items => (items.Count(x => x.FileType.ToLower().Contains(@"pdf") || x.FileType.ToLower().Contains(@"chm"))) > 1);
                     //.Where(items => (items.Count(x => !x.FilePath.Contains(@"Android应用框架原理与程序设计(简体版加源码)"))) > 1);
                     //.Where(items => (items.Count() > 1);
            var outputList = resList.SelectMany(item1 => item1).ToList();

            var sIdx = resourceRootPath.LastIndexOf(@"\", System.StringComparison.Ordinal);
            var eIdx = resourceRootPath.LastIndexOf(@".", System.StringComparison.Ordinal);
            var outputname =
                resourceRootPath.Substring(sIdx + 1,eIdx - sIdx);
                
            var xmlSerialize1 = new XmlSerialize<List<FileItem>>();
            xmlSerialize1.ToXmlFile(outputList, outputname + "_SameResource");
        }

        public List<typeItem> GetFileTypeList(ResourceRoot resourceRoot)
        {
            

            var resList =
                resourceRoot.Items
                             .GroupBy(item => item.FileType.ToLower())
                                 .OrderByDescending(x => x.Count()); //获得层级集合

            //return resList.Select(item => item.Key+"#"+item.Count().ToString()).ToList();
            return resList.Select(item => new typeItem()
            {
                filetype = item.Key ,
                count = item.Count().ToString()
            } ).ToList();

        }

        public void GetFileTypeList(String resourceRootPath)
        {
            var xmlSerialize = new XmlSerialize<ResourceRoot>();
            var resourceRoot =
                xmlSerialize.FromXmlFile(resourceRootPath);


            var resList =
                resourceRoot.Items
                             .GroupBy(item => item.FileType.ToLower())
                                 .OrderByDescending(x=>x.Count()); //获得层级集合

            var typelist = resList.Select(item => new string[]
                            {
                                item.Key, item.Count().ToString(CultureInfo.InvariantCulture)
                            }).ToList();


            //var typelist1 = resList.SelectMany(x=>x.Key)
            //var typelist = resList.Select(item => item.Key).ToList();

            var xmlSerializeW = new XmlSerialize<List<string[]>>();

            xmlSerializeW.ToXmlFile(typelist, "TypeList");
        }

        public void GetFileNameTagList(String resourceRootPath)
        {
            var xmlSerialize = new XmlSerialize<ResourceRoot>();
            var resourceRoot =
                xmlSerialize.FromXmlFile(resourceRootPath);

            foreach (var item in resourceRoot.Items)
                if (!string.IsNullOrEmpty(item.FileType))
                    item.FileName = item.FileName.Replace(item.FileType, "");

            var resList =
                resourceRoot.Items
                    //.GroupBy(item => item.FileName.Replace(item.FileType,"").Split().Where(x=>!String.IsNullOrEmpty(x))); //获得层级集合
                    .GroupBy(item => item.FileName.Split().SelectMany(x=>x.Split('-'))); //获得层级集合

            var typelist = resList.SelectMany(item => item.Key).ToList();//双循环

            var xmlSerializeW = new XmlSerialize<List<string>>();

            xmlSerializeW.ToXmlFile(typelist,"");
        }


    }
}
